﻿/**  版本信息模板在安装目录下，可自行修改。
* WeatherInfoDAL.cs
*
* 功 能： N/A
* 类 名： WeatherInfoDAL
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2016/10/12 9:39:47   N/A    初版
*
* Copyright (c) 2012 PersonBlog Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using PersonBlog.DBUtility;//Please add references
namespace PersonBlog.DAL
{
	/// <summary>
	/// 数据访问类:WeatherInfoDAL
	/// </summary>
	public partial class WeatherInfoDAL
	{
		public WeatherInfoDAL()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Id", "WeatherInfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from WeatherInfo");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(PersonBlog.Model.WeatherInfoEntity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into WeatherInfo(");
			strSql.Append("CityName,WeatherCode,WeatherDateTime,Temperature,Weather,WindDir,PicOne,PicTwo,DayLive,Other,AfterTem,AfterWeather,AfterWindDir,AfterPicOne,AfterPicTwo,DatTem,DatWeather,DatWindDir,DatPicOne,DatPicTwo)");
			strSql.Append(" values (");
			strSql.Append("@CityName,@WeatherCode,@WeatherDateTime,@Temperature,@Weather,@WindDir,@PicOne,@PicTwo,@DayLive,@Other,@AfterTem,@AfterWeather,@AfterWindDir,@AfterPicOne,@AfterPicTwo,@DatTem,@DatWeather,@DatWindDir,@DatPicOne,@DatPicTwo)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@CityName", SqlDbType.VarChar,20),
					new SqlParameter("@WeatherCode", SqlDbType.VarChar,5),
					new SqlParameter("@WeatherDateTime", SqlDbType.DateTime),
					new SqlParameter("@Temperature", SqlDbType.VarChar,10),
					new SqlParameter("@Weather", SqlDbType.VarChar,100),
					new SqlParameter("@WindDir", SqlDbType.VarChar,100),
					new SqlParameter("@PicOne", SqlDbType.VarChar,10),
					new SqlParameter("@PicTwo", SqlDbType.VarChar,10),
					new SqlParameter("@DayLive", SqlDbType.VarChar,1000),
					new SqlParameter("@Other", SqlDbType.VarChar,8000),
					new SqlParameter("@AfterTem", SqlDbType.VarChar,10),
					new SqlParameter("@AfterWeather", SqlDbType.VarChar,100),
					new SqlParameter("@AfterWindDir", SqlDbType.VarChar,100),
					new SqlParameter("@AfterPicOne", SqlDbType.VarChar,10),
					new SqlParameter("@AfterPicTwo", SqlDbType.VarChar,10),
					new SqlParameter("@DatTem", SqlDbType.VarChar,10),
					new SqlParameter("@DatWeather", SqlDbType.VarChar,100),
					new SqlParameter("@DatWindDir", SqlDbType.VarChar,100),
					new SqlParameter("@DatPicOne", SqlDbType.VarChar,10),
					new SqlParameter("@DatPicTwo", SqlDbType.VarChar,10)};
			parameters[0].Value = model.CityName;
			parameters[1].Value = model.WeatherCode;
			parameters[2].Value = model.WeatherDateTime;
			parameters[3].Value = model.Temperature;
			parameters[4].Value = model.Weather;
			parameters[5].Value = model.WindDir;
			parameters[6].Value = model.PicOne;
			parameters[7].Value = model.PicTwo;
			parameters[8].Value = model.DayLive;
			parameters[9].Value = model.Other;
			parameters[10].Value = model.AfterTem;
			parameters[11].Value = model.AfterWeather;
			parameters[12].Value = model.AfterWindDir;
			parameters[13].Value = model.AfterPicOne;
			parameters[14].Value = model.AfterPicTwo;
			parameters[15].Value = model.DatTem;
			parameters[16].Value = model.DatWeather;
			parameters[17].Value = model.DatWindDir;
			parameters[18].Value = model.DatPicOne;
			parameters[19].Value = model.DatPicTwo;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(PersonBlog.Model.WeatherInfoEntity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update WeatherInfo set ");
			strSql.Append("CityName=@CityName,");
			strSql.Append("WeatherCode=@WeatherCode,");
			strSql.Append("WeatherDateTime=@WeatherDateTime,");
			strSql.Append("Temperature=@Temperature,");
			strSql.Append("Weather=@Weather,");
			strSql.Append("WindDir=@WindDir,");
			strSql.Append("PicOne=@PicOne,");
			strSql.Append("PicTwo=@PicTwo,");
			strSql.Append("DayLive=@DayLive,");
			strSql.Append("Other=@Other,");
			strSql.Append("AfterTem=@AfterTem,");
			strSql.Append("AfterWeather=@AfterWeather,");
			strSql.Append("AfterWindDir=@AfterWindDir,");
			strSql.Append("AfterPicOne=@AfterPicOne,");
			strSql.Append("AfterPicTwo=@AfterPicTwo,");
			strSql.Append("DatTem=@DatTem,");
			strSql.Append("DatWeather=@DatWeather,");
			strSql.Append("DatWindDir=@DatWindDir,");
			strSql.Append("DatPicOne=@DatPicOne,");
			strSql.Append("DatPicTwo=@DatPicTwo");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@CityName", SqlDbType.VarChar,20),
					new SqlParameter("@WeatherCode", SqlDbType.VarChar,5),
					new SqlParameter("@WeatherDateTime", SqlDbType.DateTime),
					new SqlParameter("@Temperature", SqlDbType.VarChar,10),
					new SqlParameter("@Weather", SqlDbType.VarChar,100),
					new SqlParameter("@WindDir", SqlDbType.VarChar,100),
					new SqlParameter("@PicOne", SqlDbType.VarChar,10),
					new SqlParameter("@PicTwo", SqlDbType.VarChar,10),
					new SqlParameter("@DayLive", SqlDbType.VarChar,1000),
					new SqlParameter("@Other", SqlDbType.VarChar,8000),
					new SqlParameter("@AfterTem", SqlDbType.VarChar,10),
					new SqlParameter("@AfterWeather", SqlDbType.VarChar,100),
					new SqlParameter("@AfterWindDir", SqlDbType.VarChar,100),
					new SqlParameter("@AfterPicOne", SqlDbType.VarChar,10),
					new SqlParameter("@AfterPicTwo", SqlDbType.VarChar,10),
					new SqlParameter("@DatTem", SqlDbType.VarChar,10),
					new SqlParameter("@DatWeather", SqlDbType.VarChar,100),
					new SqlParameter("@DatWindDir", SqlDbType.VarChar,100),
					new SqlParameter("@DatPicOne", SqlDbType.VarChar,10),
					new SqlParameter("@DatPicTwo", SqlDbType.VarChar,10),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.CityName;
			parameters[1].Value = model.WeatherCode;
			parameters[2].Value = model.WeatherDateTime;
			parameters[3].Value = model.Temperature;
			parameters[4].Value = model.Weather;
			parameters[5].Value = model.WindDir;
			parameters[6].Value = model.PicOne;
			parameters[7].Value = model.PicTwo;
			parameters[8].Value = model.DayLive;
			parameters[9].Value = model.Other;
			parameters[10].Value = model.AfterTem;
			parameters[11].Value = model.AfterWeather;
			parameters[12].Value = model.AfterWindDir;
			parameters[13].Value = model.AfterPicOne;
			parameters[14].Value = model.AfterPicTwo;
			parameters[15].Value = model.DatTem;
			parameters[16].Value = model.DatWeather;
			parameters[17].Value = model.DatWindDir;
			parameters[18].Value = model.DatPicOne;
			parameters[19].Value = model.DatPicTwo;
			parameters[20].Value = model.Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from WeatherInfo ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from WeatherInfo ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public PersonBlog.Model.WeatherInfoEntity GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Id,CityName,WeatherCode,WeatherDateTime,Temperature,Weather,WindDir,PicOne,PicTwo,DayLive,Other,AfterTem,AfterWeather,AfterWindDir,AfterPicOne,AfterPicTwo,DatTem,DatWeather,DatWindDir,DatPicOne,DatPicTwo from WeatherInfo ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			PersonBlog.Model.WeatherInfoEntity model=new PersonBlog.Model.WeatherInfoEntity();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public PersonBlog.Model.WeatherInfoEntity DataRowToModel(DataRow row)
		{
			PersonBlog.Model.WeatherInfoEntity model=new PersonBlog.Model.WeatherInfoEntity();
			if (row != null)
			{
				if(row["Id"]!=null && row["Id"].ToString()!="")
				{
					model.Id=int.Parse(row["Id"].ToString());
				}
				if(row["CityName"]!=null)
				{
					model.CityName=row["CityName"].ToString();
				}
				if(row["WeatherCode"]!=null)
				{
					model.WeatherCode=row["WeatherCode"].ToString();
				}
				if(row["WeatherDateTime"]!=null && row["WeatherDateTime"].ToString()!="")
				{
					model.WeatherDateTime=DateTime.Parse(row["WeatherDateTime"].ToString());
				}
				if(row["Temperature"]!=null)
				{
					model.Temperature=row["Temperature"].ToString();
				}
				if(row["Weather"]!=null)
				{
					model.Weather=row["Weather"].ToString();
				}
				if(row["WindDir"]!=null)
				{
					model.WindDir=row["WindDir"].ToString();
				}
				if(row["PicOne"]!=null)
				{
					model.PicOne=row["PicOne"].ToString();
				}
				if(row["PicTwo"]!=null)
				{
					model.PicTwo=row["PicTwo"].ToString();
				}
				if(row["DayLive"]!=null)
				{
					model.DayLive=row["DayLive"].ToString();
				}
				if(row["Other"]!=null)
				{
					model.Other=row["Other"].ToString();
				}
				if(row["AfterTem"]!=null)
				{
					model.AfterTem=row["AfterTem"].ToString();
				}
				if(row["AfterWeather"]!=null)
				{
					model.AfterWeather=row["AfterWeather"].ToString();
				}
				if(row["AfterWindDir"]!=null)
				{
					model.AfterWindDir=row["AfterWindDir"].ToString();
				}
				if(row["AfterPicOne"]!=null)
				{
					model.AfterPicOne=row["AfterPicOne"].ToString();
				}
				if(row["AfterPicTwo"]!=null)
				{
					model.AfterPicTwo=row["AfterPicTwo"].ToString();
				}
				if(row["DatTem"]!=null)
				{
					model.DatTem=row["DatTem"].ToString();
				}
				if(row["DatWeather"]!=null)
				{
					model.DatWeather=row["DatWeather"].ToString();
				}
				if(row["DatWindDir"]!=null)
				{
					model.DatWindDir=row["DatWindDir"].ToString();
				}
				if(row["DatPicOne"]!=null)
				{
					model.DatPicOne=row["DatPicOne"].ToString();
				}
				if(row["DatPicTwo"]!=null)
				{
					model.DatPicTwo=row["DatPicTwo"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,CityName,WeatherCode,WeatherDateTime,Temperature,Weather,WindDir,PicOne,PicTwo,DayLive,Other,AfterTem,AfterWeather,AfterWindDir,AfterPicOne,AfterPicTwo,DatTem,DatWeather,DatWindDir,DatPicOne,DatPicTwo ");
			strSql.Append(" FROM WeatherInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Id,CityName,WeatherCode,WeatherDateTime,Temperature,Weather,WindDir,PicOne,PicTwo,DayLive,Other,AfterTem,AfterWeather,AfterWindDir,AfterPicOne,AfterPicTwo,DatTem,DatWeather,DatWindDir,DatPicOne,DatPicTwo ");
			strSql.Append(" FROM WeatherInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM WeatherInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from WeatherInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "WeatherInfo";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 获得最新的天气数据
        /// </summary>
        public DataSet GetNewList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append( "with AA as (SELECT MAX(id) AS Id1 FROM WeatherInfo GROUP BY CityName),BB AS (SELECT * FROM WeatherInfo)SELECT BB.*FROM AA,BB WHERE BB.Id IN (AA.Id1)");
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  ExtensionMethod
    }
}

